package com.hjq.www.Dao.Impl;

import com.hjq.www.Dao.AnimalDao;
import com.hjq.www.utils.JDBCUtils;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Create with IntelliJ IDEA
 *
 * @author: hjq
 * @Date:
 * @Description:
 */
public class AnimalDaoImpl implements AnimalDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    private JTextField idField;
    private JTextField nameField;
    private JTextField typeField;
    private JTextField introductionField;
    private JTextField ageField;
    private JTextField statusField;
    private JTextField zidField;

    /**
     * 添加动物信息
     * @param idField
     * @param nameField
     * @param typeField
     * @param ageField
     * @param introductionField
     * @param statusField
     * @param zidField
     */
    @Override
    public void add(JTextField idField, JTextField nameField, JTextField typeField, JTextField ageField, JTextField introductionField, JTextField statusField, JTextField zidField) {
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into animals(id,name,type,age,introduction,status,zid) value(?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, idField.getText());
            ps.setString(2, nameField.getText());
            ps.setString(3, typeField.getText());
            ps.setString(4, ageField.getText());
            ps.setString(5, introductionField.getText());
            ps.setString(6, statusField.getText());
            ps.setString(7,zidField.getText());
            ps.executeUpdate();
            conn.commit();
            JOptionPane.showMessageDialog(null, "添加成功");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "添加失败");
            e.printStackTrace();
        }
    }

    /**
     * 查询动物信息
     * @param idField
     * @param nameField
     * @param typeField
     * @param introduction
     * @param ageField
     * @param statusField
     * @param zidField
     */
    @Override
    public void search(JTextField idField, JTextField nameField, JTextField typeField, JTextField introduction, JTextField ageField, JTextField statusField, JTextField zidField) {
        String sid=idField.getText();
        boolean flag=false;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "select * from animals where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, sid);
            rs = ps.executeQuery();
            while (rs.next()) {
                String ssid = rs.getString("id");
                if (sid.equals(ssid)) {
                    idField.setText(rs.getString("id"));
                    nameField.setText(rs.getString("name"));
                    typeField.setText(rs.getString("type"));
                    ageField.setText(rs.getString("age"));
                    introduction.setText(rs.getString("introduction"));
                    statusField.setText(rs.getString("status"));
                    zidField.setText(rs.getString("zid"));
                    flag=true;
                }
            }
            if(flag){
                JOptionPane.showMessageDialog(null, "查询成功");
            }else{JOptionPane.showMessageDialog(null,"查询失败");}
        }catch (SQLException e){
                JOptionPane.showMessageDialog(null, "查询失败");
                e.printStackTrace();
            }finally{
            JDBCUtils.close(conn,ps,rs);
        }
    }

    /**
     * 修改动物信息
     * @param idField
     * @param nameField
     * @param typeField
     * @param introductionField
     * @param ageField
     * @param statusField
     * @param zidField
     */

    @Override
    public void update(JTextField idField, JTextField nameField, JTextField typeField, JTextField introductionField, JTextField ageField, JTextField statusField, JTextField zidField) {
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "update breeder set id=?,username=?,password=?,gender=?,phone=?,idnum=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,idField.getText());
            ps.setString(2,nameField.getText());
            ps.setString(3,typeField.getText());
            ps.setString(4,ageField.getText());
            ps.setString(5,introductionField.getText());
            ps.setString(6,statusField.getText());
            ps.setString(7,zidField.getText());
            ps.executeUpdate();
            JOptionPane.showMessageDialog(null, "修改成功");
        }catch (SQLException e){
            JOptionPane.showMessageDialog(null, "修改失败");
            e.printStackTrace();
        }
    }

    /**
     * 删除动物信息
     * @param idField
     */
    @Override
    public void delete(JTextField idField){
        String sno = idField.getText();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "delete from animals where id =?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,sno);
            ps.execute();
            JOptionPane.showMessageDialog(null,"删除成功");
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"删除失败");
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn,ps);
        }
    }
    /**
     * 重置操作
     */
    @Override
    public void reset(){
     idField.setText("");
     nameField.setText("");
     typeField.setText("");
     ageField.setText("");
     introductionField.setText("");
     statusField.setText("");
     zidField.setText("");
    }
}
